package com.pctx.bibatracker.service.impl;

import com.pctx.bibacommon.result.Result;
import com.pctx.bibacommon.util.UuidUtil;
import com.pctx.bibatracker.dao.FenceMapper;
import com.pctx.bibatracker.pojo.Fence;
import com.pctx.bibatracker.service.FenceService;
import com.pctx.bibatracker.util.CoordinateTransforToGaoDe;
import com.pctx.bibatracker.util.CoordinateTransform;
import com.pctx.bibatracker.util.MathUtils;
import com.sun.org.apache.xpath.internal.operations.Bool;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("/fenceService")
public class FenceServiceImpl implements FenceService {
    @Autowired
    private FenceMapper fenceMapper;

    /**
     * 添加围栏（百度地图）
     *
     * @param deviceNo
     * @param address
     * @param radius
     * @param fence_name
     * @return
     * @throws Exception
     */
    @Override
    public Result addFence(String deviceNo, String address, Long radius, String fence_name,String type) throws Exception {
        if (StringUtils.isEmpty(deviceNo)) {
            return Result.error("参数不能为空");
        }
        String cord = CoordinateTransform.tansferAddress(address);
        //地址转换成百度坐标；
        JSONObject jsonObject = JSONObject.fromObject(cord);
        //System.out.println(jsonObject+"kkkkk");
        JSONObject result = jsonObject.getJSONObject("result");
        JSONObject resultone = result.getJSONObject("location");
        BigDecimal cordx = new BigDecimal(resultone.getString("lng"));
       // System.out.println(cordx+"kkkkk======");
        BigDecimal cordy = new BigDecimal(resultone.getString("lat"));
        Fence fence = new Fence();
        Date date = new Date();
        fence.setUuid(UuidUtil.randomUUID());
        if(type.equals("进入")){
            fence.setInAlarm(0);
        }else if(type.equals("出去")){
            fence.setInAlarm(1);
        }else{
            fence.setInAlarm(2);
        }
        //fence.setInAlarm(0);
        //fence.setOutAlarm(1);
        fence.setStartPoint(address);
        fence.setFenceName(fence_name);
        fence.setDeviceUuid(deviceNo);
        fence.setCreateTime(date);
        fence.setUpdateTime(date);
        fence.setCordX(cordx);
        fence.setCordY(cordy);
        fence.setCordType((byte) 1);
        fence.setCordZ(new BigDecimal(29.57908428837437));
        fence.setRadius(radius * 1000);
        fence.setRemark("aa");
        Boolean flag = fenceMapper.insertSelective(fence) > 0;
        if (flag) {
            return Result.success(fence, "围栏添加成功");
        } else {
            return Result.error("围栏添加失败");
        }
    }

    /**
     * 围栏列表
     *
     * @param deviceNo
     * @return
     * @throws Exception
     */
    @Override
    public Result selectFence(String deviceNo) throws Exception {
        if (StringUtils.isEmpty(deviceNo)) {
            return Result.error("参数不能为空");
        }
        List<Fence> list = fenceMapper.selectBydeviceUuid(deviceNo);
        return Result.success(list, "围栏列表查询成功");

    }

    /**
     * 添加围栏（高德地图）
     *
     * @param deviceNo
     * @param address
     * @param radius
     * @param fence_name
     * @return
     * @throws Exception
     */
    @Override
    public Result addGaodeFence(String deviceNo, String address, Long radius, String fence_name) throws Exception {
        String cord = CoordinateTransforToGaoDe.tansferAddress(address);//地址转换成高德坐标；
        JSONObject jsonObject = JSONObject.fromObject(cord);
        ArrayList list = new ArrayList();
        JSONArray result = jsonObject.getJSONArray("geocodes");
        Object[] os = result.toArray();
        for (int i = 0; i < os.length; i++) {
            JSONObject jsonObj = JSONObject.fromObject(os[i]);
            String b = jsonObj.get("location").toString();
            System.out.println(b);
            String[] h = b.split(",");
            list.add(h[0]);
            list.add(h[1]);
        }
        //System.out.println(list.get(0)+"sssssssss");
        BigDecimal gd_x = MathUtils.getBigDecimal(list.get(0)).setScale(12, BigDecimal.ROUND_HALF_UP);
        //System.out.println(gd_x+"sssssssss====");
        BigDecimal gd_y = MathUtils.getBigDecimal(list.get(1)).setScale(12, BigDecimal.ROUND_HALF_UP);
        Fence fence = new Fence();
        fence.setUuid(UuidUtil.randomUUID());
        fence.setInAlarm(0);
        fence.setOutAlarm(1);
        fence.setStartPoint(address);
        fence.setFenceName(fence_name);
        fence.setDeviceUuid(deviceNo);
        Date date = new Date();
        fence.setCreateTime(date);
        fence.setUpdateTime(date);
        fence.setCordX(gd_x);
        fence.setCordY(gd_y);
        fence.setCordType((byte) 2);
        fence.setCordZ(new BigDecimal(29.57908428837437));
        fence.setRadius(radius * 1000);
        fence.setRemark("aa");
        Boolean flag = fenceMapper.insertSelective(fence) > 0;
        if (flag) {
            return Result.success(fence, "围栏添加成功");
        } else {
            return Result.error("围栏添加失败");
        }
    }

    @Override
    public Result deleteFence(String deviceNo, String fence_name) throws Exception {
        if(StringUtils.isEmpty(deviceNo)){
            return Result.error("参数不能为空");
        }
        System.out.println("11111");
        Boolean flag=fenceMapper.deleteFence(deviceNo,fence_name)>0;
        System.out.println(flag);
        if(flag){
            return Result.success("删除围栏成功");
        }else{
            return Result.error("围栏删除失败");
        }

    }
}
